import { Request, Response } from 'express';

// 模拟日志数据
const logs: any[] = [
  { id: 1, message: '系统启动', level: 'info', timestamp: '2023-10-01T10:00:00Z' },
  { id: 2, message: '用户登录', level: 'info', timestamp: '2023-10-01T10:05:00Z' },
  { id: 3, message: '用户登出', level: 'info', timestamp: '2023-10-01T10:10:00Z' },
  { id: 4, message: '系统错误', level: 'error', timestamp: '2023-10-01T10:15:00Z' },
];

// 模拟分页数据
const getPageData = (params: any) => {
  const { current = 1, pageSize = 10 } = params;
  const start = (current - 1) * pageSize;
  const end = start + Number(pageSize);
  return {
    content: logs.slice(start, end),
      totalElements: logs.length,
    current: Number(current),
    pageSize: Number(pageSize),
  };
};

export default {
  // 模拟获取日志列表接口
  'GET /api/system/log': (req: Request, res: Response) => {
    const pageData = getPageData(req.query);
    res.status(200).json({
      code: 200,
      message: '获取日志列表成功',
      data: pageData,
    });
  },

  // 模拟添加日志接口
  'POST /api/system/log': (req: Request, res: Response) => {
    const newLog = req.body;
    newLog.id = logs.length + 1;
    logs.push(newLog);
    res.status(200).json({
      code: 200,
      message: '添加日志成功',
      data: newLog,
    });
  },

  // 模拟更新日志接口
  'PUT /api/system/log': (req: Request, res: Response) => {
    const updatedLog = req.body;
    const index = logs.findIndex((log) => log.id === updatedLog.id);
    if (index !== -1) {
      logs[index] = updatedLog;
      res.status(200).json({
        code: 200,
        message: '更新日志成功',
        data: updatedLog,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '日志不存在',
      });
    }
  },

  // 模拟删除日志接口
  'DELETE /api/system/log/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const index = logs.findIndex((log) => log.id === id);
    if (index !== -1) {
      logs.splice(index, 1);
      res.status(200).json({
        code: 200,
        message: '删除日志成功',
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '日志不存在',
      });
    }
  },

  // 模拟根据日志 ID 获取单个日志详细信息接口
  'GET /api/system/log/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const log = logs.find((log) => log.id === id);
    if (log) {
      res.status(200).json({
        code: 200,
        message: '获取日志详情成功',
        data: log,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '日志不存在',
      });
    }
  },
};
